<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\model\T_WMS_STOCK_DOC;
use app\model\T_WMS_STOCK_INFO;
use app\model\T_WMS_STOCK_ITEM;
use app\Server\Outboundkb;
use think\Db;
use think\Request;

class Logisticskb extends BaseController
{
    /**
     * 显示资源列表
     *入库单看板
     * @return \think\Response
     */
    public function index()
    {
        //获取待入库任务单（根据类型分类：采购入库、完工入库、其他入库）
        //采购入库IP,状态：开立1，操作中2,单据类型：DJ02
        $data1=$this->commend('IP',[1,2],'DJ02','CREATE_TIME');
        //今日已入库
        $data2=$this->commends('IP',[3,4],'DJ02','CLOSE_TIME');
        //昨日入库单据数和数量
        $count3=$this->commend2('IP',[3,4],'DJ02');
        //待上架物料清单型号和数量，物料数量和型号数
        //库存物料信息(T_WMS_STOCK_INFO)
         $data3=$this->commend3([['STATUS','=',13],['IN_DOC','like','IP%']]);
        //今日已上架物料清单型号和数量，物料数量和型号数
          $data4=$this->commend3([['STATUS','=',2],['IN_DOC','like','IP%'],['INSTOCK_TIME','>=',date('Y-m-d 00:00:00')],['INSTOCK_TIME','<=',date('Y-m-d 23:59:59')]]);


          //完工入库IM,状态：开立1，操作中2,单据类型：DJ02
        $data5=$this->commend('IM',[1,2],'DJ05','CREATE_TIME');
        //今日已入库
        $data6=$this->commends('IM',[3,4],'DJ05','CLOSE_TIME');
        //昨日入库单据数和数量
        $count8=$this->commend2('IM',[3,4],'DJ05');
        //待上架物料清单型号和数量，物料数量和型号数
        //库存物料信息(T_WMS_STOCK_INFO)
        $data7=$this->commend3([['STATUS','=',13],['IN_DOC','like','IM%']]);
        //今日已上架物料清单型号和数量，物料数量和型号数
        $data8=$this->commend3([['STATUS','=',2],['IN_DOC','like','IM%'],['INSTOCK_TIME','>=',date('Y-m-d 00:00:00')],['INSTOCK_TIME','<=',date('Y-m-d 23:59:59')]]);


        //其他入库IO,状态：开立1，操作中2,单据类型：DJ02
        $data9=$this->commend('IO',[1,2],'DJ06','CREATE_TIME');
        //今日已入库
        $data10=$this->commends('IO',[3,4],'DJ06','CLOSE_TIME');
        //昨日入库单据数和数量
        $count13=$this->commend2('IO',[3,4],'DJ06');
        //待上架物料清单型号和数量，物料数量和型号数
        //库存物料信息(T_WMS_STOCK_INFO)
        $data11=$this->commend3([['STATUS','=',13],['IN_DOC','like','IO%']]);
        //今日已上架物料清单型号和数量，物料数量和型号数
        $data12=$this->commend3([['STATUS','=',2],['IN_DOC','like','IO%'],['INSTOCK_TIME','>=',date('Y-m-d 00:00:00')],['INSTOCK_TIME','<=',date('Y-m-d 23:59:59')]]);
        


        //生产退料入库SL状态：开立1，操作中2,单据类型：DJ02
        $data13=$this->commend('SL',[1,2],'DJ03','CREATE_TIME');
        //今日已入库
        $data14=$this->commends('SL',[3,4],'DJ03','CLOSE_TIME');
        //昨日入库单据数和数量
        $count17=$this->commend2('SL',[3,4],'DJ03');
        //待上架物料清单型号和数量，物料数量和型号数
        //库存物料信息(T_WMS_STOCK_INFO)
        $data15=$this->commend3([['STATUS','=',13],['IN_DOC','like','SL%']]);
        //今日已上架物料清单型号和数量，物料数量和型号数
        $data16=$this->commend3([['STATUS','=',2],['IN_DOC','like','SL%'],['INSTOCK_TIME','>=',date('Y-m-d 00:00:00')],['INSTOCK_TIME','<=',date('Y-m-d 23:59:59')]]);

        
        return view('/PUT',['data1'=>$data1,'data2'=>$data2,'count3'=>$count3,'data3'=>$data3,'data4'=>$data4,'data5'=>$data5,'data6'=>$data6,'count8'=>$count8,'data7'=>$data7,'data8'=>$data8,'data9'=>$data9,'data10'=>$data10,'count13'=>$count13,'data11'=>$data11,'data12'=>$data12,'data13'=>$data13,'data14'=>$data14,'data15'=>$data15,'data16'=>$data16,'count17'=>$count17]);
    }



    public function commend($code='',$statu=[],$type='',$orderfield=''){
        //获取待入库明细
        $list=(new T_WMS_STOCK_DOC())->alias('a')
            ->leftjoin('T_CO_SUPPLIER b','a.SUPPLIER_CODE=b.SUPPLIER_CODE')
            ->field('a.DOC_NO,a.DOC_STATUS,b.SUPPLIER_NAME,a.PLAN_RECEIVE_DATE,a.CLOSE_TIME,a.CREATE_TIME,a.MEMO')
            ->where([['DOC_NO','like',$code.'%'],['DOC_STATUS','in',$statu],['DOC_TYPE','=',"$type"],['a.PLAN_RECEIVE_DATE','>=',date('Y-m-d 00:00:00',strtotime('-60 day'))],['a.PLAN_RECEIVE_DATE','<=',date('Y-m-d 23:59:59')]])
            ->order($orderfield,'DESC')
            ->select();
        $list=$list->toArray();
        //待入库单据数
        $count=count($list);
        foreach($list as $key=>$v){
            $list[$key]['PLAN_RECEIVE_DATE']=substr((string)$v['PLAN_RECEIVE_DATE'],0,16);
            $list[$key]['CLOSE_TIME']=substr((string)$v['CLOSE_TIME'],0,16);
            $list[$key]['CREATE_TIME']=substr((string)$v['CREATE_TIME'],0,16);
        }
        $data=['list'=>$list,'count'=>$count];
        return $data;
    }
    public function commends($code='',$statu=[],$type='',$orderfield=''){
        //获取已入库明细
        $list=(new T_WMS_STOCK_DOC())->alias('a')
            ->leftjoin('T_CO_SUPPLIER b','a.SUPPLIER_CODE=b.SUPPLIER_CODE')
            ->field('a.DOC_NO,a.DOC_STATUS,b.SUPPLIER_NAME,a.PLAN_RECEIVE_DATE,a.CLOSE_TIME,a.CREATE_TIME,a.MEMO')
            ->where([['DOC_NO','like',$code.'%'],['DOC_STATUS','in',$statu],['DOC_TYPE','=',"$type"],['a.CLOSE_TIME','>=',date('Y-m-d 00:00:00')],['a.CLOSE_TIME','<=',date('Y-m-d 23:59:59')]])
            ->order($orderfield,'DESC')
            ->select();
        $list=$list->toArray();
        foreach($list as $key=>$v){
            $list[$key]['PLAN_RECEIVE_DATE']=substr((string)$v['PLAN_RECEIVE_DATE'],0,16);
            $list[$key]['CLOSE_TIME']=substr((string)$v['CLOSE_TIME'],0,16);
            $list[$key]['CREATE_TIME']=substr((string)$v['CREATE_TIME'],0,16);
        }
        //已入库单据数
        $count=count($list);
        //已入库物料总个数
        $counts=(new T_WMS_STOCK_ITEM())->alias('a')
            ->leftJoin('T_WMS_STOCK_DOC b','a.DOC_NO=b.DOC_NO')
            ->where([['b.DOC_NO','like',$code.'%'],['b.DOC_STATUS','in',$statu],['b.DOC_TYPE','=',"$type"],['b.CLOSE_TIME','>=',date('Y-m-d 00:00:00')],['b.CLOSE_TIME','<=',date('Y-m-d 23:59:59')]])
            ->field('a.ID')->count();
        $data=['list'=>$list,'count'=>$count,'counts'=>$counts];
        return $data;
    }
    public function commend2($code='',$statu=[],$type=''){
        //计算昨日入库数量和单据数
        $count=(new T_WMS_STOCK_DOC())
            ->field('ID')
            ->where([['DOC_NO','like',$code.'%'],['DOC_STATUS','in',$statu],['DOC_TYPE','=',"$type"],['CLOSE_TIME','>=',date('Y-m-d 00:00:00',strtotime('-1 day'))],['CLOSE_TIME','<=',date('Y-m-d 23:59:59',strtotime('-1 day'))]])
            ->count();
            //物料数量
            $counts=(new T_WMS_STOCK_ITEM())->alias('a')
            ->leftJoin('T_WMS_STOCK_DOC b','a.DOC_NO=b.DOC_NO')
            ->where([['b.DOC_NO','like',$code.'%'],['b.DOC_STATUS','in',$statu],['b.DOC_TYPE','=',"$type"],['b.CLOSE_TIME','>=',date('Y-m-d 00:00:00',strtotime('-1 day'))],['b.CLOSE_TIME','<=',date('Y-m-d 23:59:59',strtotime('-1 day'))]])
            ->field('a.ID')->count();
        return ['count'=>$count,'counts'=>$counts];
    }
    public function commend3($where=[]){
        //待上架物料清单型号和数量，物料数量和型号数
        //今日已上架物料清单型号和数量，物料数量和型号数
        //库存物料信息(T_WMS_STOCK_INFO)
        $list=(new T_WMS_STOCK_INFO())->alias('a')
            ->leftjoin('T_CO_ITEM b','a.ITEM_CODE=b.ITEM_CODE')
            ->where($where)
//            ->field('a.ITEM_QTY,b.ITEM_MODEL')
            ->field('a.ITEM_QTY,b.ITEM_CODE')
            ->select();
        $list=$list->toArray();
        $count=count($list);
        $new=[];
        foreach ($list as $k=>$v){
            $name=$v['ITEM_CODE'];
            $num=$v['ITEM_QTY'];
            if(array_key_exists($name,$new)){
                $new[$name]+=$num;
            }else{
                $new[$name]=$num;
            }
        }
        $news=[];
        foreach ($new as $k=>$v){
            $news[]=['ITEM_CODE'=>$k,'ITEM_QTY'=>intval($v)];
        }
        $data=['list'=>$news,'count'=>$count,'counts'=>count($news)];
        return $data;
    }

    /**
     * 出库单看板
     * 生产领料、销售发货
     */
    public function Outboundkb(){
        //领料出库
        //当前待出库任务单（根据单据类型分类）
        //当前待出库数量（总个数和单据数）
          $data1=(new Outboundkb())->index();
        return view('/OUT',['data1'=>$data1['data1'],'data2'=>$data1['data2'],'count3'=>$data1['count3'],'count3s1'=>$data1['count3s1'],'count3s2'=>$data1['count3s2'],'count3s3'=>$data1['count3s3']]);
    }
    public function Outboundkbxs(){
        //销售发货
        //待出库,今日已出库，昨日出库数量统计
          $data2=(new Outboundkb())->index2();
        return view('/OUTXS',['data3'=>$data2['data3'],'data4'=>$data2['data4'],'count4'=>$data2['count4'],'count4s'=>$data2['count4s']]);
    }



}
